package com.neusoft.bizcore.auth.converter;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import com.neusoft.bizcore.auth.dto.AuthUserCreateUpdateDTO;
import com.neusoft.bizcore.auth.dto.AuthUserDTO;
import com.neusoft.bizcore.auth.model.AuthOganization;
import com.neusoft.bizcore.auth.model.AuthPermission;
import com.neusoft.bizcore.auth.model.AuthRole;
import com.neusoft.bizcore.auth.model.AuthUser;
import com.neusoft.bizcore.auth.repository.AuthOganizationRepository;
import com.neusoft.bizcore.auth.repository.AuthPermissionRepository;
import com.neusoft.bizcore.auth.repository.AuthRoleRepository;
import com.neusoft.bizcore.auth.repository.AuthUserRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;

@Component
public class AuthUserConverter extends AbstractConverter<AuthUser, AuthUserDTO> {

    @Autowired
    private AuthUserRepository authUserRepository;
    @Autowired
    private AuthRoleRepository authRoleRepository;
    @Autowired
    private AuthRoleConverter authRoleConverter;
    @Autowired
    private AuthOganizationRepository authOganizationRepository;
    @Autowired
    private AuthOganizationConverter authOganizationConverter;
    @Autowired
    private AuthPermissionRepository authPermissionRepository;
    @Autowired
    private AuthPermissionConverter authPermissionConverter;

    @Override
    public AuthUser toModel(final AuthUserDTO dto) {
        AuthUser model;
        if (dto.isNew()) {
            model = new AuthUser();
            model.setPassword(DigestUtils.md5DigestAsHex(dto.getPassword().getBytes()));
        } else {
            model = this.authUserRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }
        model.setUsername(dto.getUsername());
        model.setName(dto.getName());
        model.setLocked(dto.isLocked());
        model.setEnabled(dto.isEnabled());
        model.setMail(dto.getMail());
        model.setPhone(dto.getPhone());
        return model;
    }

    @Override
    public AuthUserDTO toDTO(final AuthUser model, final boolean forListView) {
        final AuthUserDTO dto = new AuthUserDTO();
        dto.setId(model.getId());
        dto.setUsername(model.getUsername());
        //dto.setPassword(model.getPassword());
        dto.setName(model.getName());
        dto.setLocked(model.isLocked());
        dto.setEnabled(model.isEnabled());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setMail(model.getMail());
        dto.setPhone(model.getPhone());
        dto.getOganizations().addAll(this.authOganizationConverter.toListDTO(model.getOganizations()));
        if (!forListView) {
            dto.getRoles().addAll(this.authRoleConverter.toListDTO(model.getRoles()));
            dto.getPermissions().addAll(this.authPermissionConverter.toListDTO(model.getPermissions()));
        }
        return dto;
    }

    public AuthUser toModel(final AuthUserCreateUpdateDTO dto) {
        AuthUser model;
        if (dto.isNew()) {
            model = new AuthUser();
            model.setPassword(DigestUtils.md5DigestAsHex(dto.getPassword().getBytes()));
        } else {
            model = this.authUserRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }
        model.setUsername(dto.getUsername());
        model.setName(dto.getName());
        model.setLocked(dto.isLocked());
        model.setEnabled(dto.isEnabled());
        model.setMail(dto.getMail());
        model.setPhone(dto.getPhone());

        model.getOganizations().clear();
        for (final String code : dto.getOganizations()) {
            final AuthOganization org = this.authOganizationRepository.findByCode(code);
            if (null != org) {
                model.getOganizations().add(org);
            }
        }
        model.getRoles().clear();
        for (final String code : dto.getRoles()) {
            final AuthRole role = this.authRoleRepository.findByCode(code);
            if (null != role) {
                model.getRoles().add(role);
            }
        }
        model.getPermissions().clear();
        for (final String code : dto.getPermissions()) {
            final AuthPermission permission = this.authPermissionRepository.findByCode(code);
            if (null != permission) {
                model.getPermissions().add(permission);
            }
        }
        return model;
    }

}
